<script setup lang="ts">
import { tagAll } from '@/api/interface/web/tag'
import type { FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'

interface Props {
  data?: Record<string, any> | never | null
  status?: string | never | null
}

const props = withDefaults(defineProps<Props>(), {
  data: null,
  status: '',
})

const baseFormRef = ref()

const form = reactive<Record<string, any>>({ enterpriseType: 1 })

const rules = reactive<FormRules>({
  name: [{ required: true, message: '知识库名称不能为空!', trigger: 'blur' }],
  tagId: [{ required: true, message: '标签不能为空!', trigger: 'blur' }],
})

const tags: any = ref([])
const getTagAll = () => {
  tagAll({}).then((res: any) => {
    tags.value = res
  })
}
getTagAll()

const initForm = () => {
  if (props.data) {
    Object.keys(props.data).forEach((key: string) => {
      form[key] = props.data![key]
    })
  }
}

const formatForm = () => {
  const result: Record<string, any> = {}
  Object.keys(form).forEach((key: string) => {
    result[key] = form[key]
  })

  return result
}

const submitForm = () => {
  return baseFormRef.value.submit(formatForm)
}

defineExpose({
  submitForm,
})

onMounted(() => {
  initForm()
})
</script>

<template>
  <div class="form-wrapper">
    <BaseForm
      ref="baseFormRef"
      :form="form"
      :rules="rules"
      width="400px"
      :attributes="{ 'label-width': '140px' }"
    >
      <el-form-item label="知识库名称:" prop="name">
        <el-input v-model="form.name" type="text" placeholder="请输入知识库名称"> </el-input>
      </el-form-item>
      <el-form-item label="标签:" prop="tagId">
        <el-select v-model="form.tagId" placeholder="请选择类型">
          <el-option v-for="item in tags" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="mark">
        <el-input v-model="form.mark" type="textarea" maxlength="50" placeholder="请输入备注" />
      </el-form-item>
    </BaseForm>
  </div>
</template>

<style lang="scss" scoped>
.form-wrapper {
  width: 400px;
}
</style>
